package com.csc.service.impl;

import com.csc.dto.ProductParam;
import com.csc.entity.ProductTypes;
import com.csc.entity.Products;
import com.csc.mapper.ProductTypesMapper;
import com.csc.mapper.ProductsMapper;
import com.csc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/6/24 下午2:57
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private ProductTypesMapper productTypesMapper;

    // 产品相关方法实现
    @Override
    public List<Products> listProducts(ProductParam productParam) {
        return productsMapper.list(productParam);
    }

    @Override
    public String deleteProduct(Integer productId) {
        return productsMapper.delete(productId)  > 0 ? "success" : "fail";
    }

    @Override
    public String addProduct(Products product) {
        try {
            // 将Products对象转换为Map
            Map<String, Object> params = new HashMap<>();
            params.put("productName",  product.getProductName());
            params.put("typeId",  product.getTypeId());
            params.put("price",  product.getPrice());
            params.put("stock",  product.getStock());
            params.put("imageUrl",  product.getImageUrl());
            params.put("status",  product.getStatus());
            params.put("description",  product.getDescription());

            // 调用Mapper方法
            int result = productsMapper.add(params);
            return result > 0 ? "success" : "fail";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail: " + e.getMessage();
        }
    }

    @Override
    public Products getProductDetail(Integer productId) {
        return productsMapper.detail(productId);
    }

    @Override
    public String updateProduct(Products products) {
        return productsMapper.update(products)  > 0 ? "success" : "fail";
    }

    // 产品类型相关方法实现
    @Override
    public List<ProductTypes> listProductTypes() {
        return productTypesMapper.list();
    }
}

